package cn.tedu.charging.cost.dao.repository.impl;

import cn.tedu.charging.cost.dao.repository.CostRuleCacheRepository;
import cn.tedu.charging.cost.pojo.po.CostRulePO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CostRuleCacheRepositoryImpl implements CostRuleCacheRepository {

    @Autowired
    //private RedisTemplate<String, Serializable> redisTemplate;
    private RedisTemplate<String, List<CostRulePO>> redisTemplate;

    @Override
    public List<CostRulePO> getCostRuleByStationId(Integer stationId) {
        ValueOperations<String, List<CostRulePO>> valueOperations = redisTemplate.opsForValue();
        String costRuleKey = "COST_RULE_KEY_" + stationId ;
        //Serializable serializable = valueOperations.get(costRuleKey);
        List<CostRulePO> costRulePOS = valueOperations.get(costRuleKey);
        /*if (serializable != null) {
            List<LinkedHashMap<String,CostRulePO>> list = JsonUtils.fromJson(String.valueOf(serializable), List.class);
            List<CostRulePO> result = new ArrayList<>();
            list.forEach(e->{
                //e  LinkedHashMap<String,CostRulePO>
                // LinkedHashMap 转换为 CostRulePO
                    CostRulePO costRulePO = JsonUtils.covertValue(e,CostRulePO.class);
                    result.add(costRulePO);
                }
            );
            return result;
        }*/
        return costRulePOS;
    }

    @Override
    public void saveCostRule(Integer stationId,List<CostRulePO> dbCostRules) {
        //ValueOperations<String, Serializable> valueOperations = redisTemplate.opsForValue();
        ValueOperations<String, List<CostRulePO>> valueOperations = redisTemplate.opsForValue();
        String costRuleKey = "COST_RULE_KEY_" + stationId ;
        //https://juejin.cn/post/7310412232613429257 json 转换是转换是需要消耗性能
        //String json = JsonUtils.toJson(dbCostRules);
        valueOperations.set(costRuleKey,dbCostRules);

    }
}
